﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Domain.Entities;
using System.Domain.IRepository;
using System.Data;

namespace Wk.System.Infrastructure.Repository
{
    public class MenuRepository : IMenuRepository
    {
        private readonly IRepository<Menu> _client;
        public MenuRepository(IRepository<Menu> client)
        {
            _client = client;
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>

        public async Task<List<Menu>> GetAllMenusAsync()
        {
            return await _client.Context.Queryable<Menu>().OrderByDescending(a => a.px).ToListAsync();
        }

        /// <summary>
        /// 获取菜单树形
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menu>> GetMenuTreeAsync(int roleId)
        {
            if (roleId == 0)
            {
                return await _client.Context.Queryable<Menu>()
                      .Select((it) => new Menu()
                      {
                          id = it.id.SelectAll()
                      })
                      .OrderByDescending(it => it.px)
                      .ToTreeAsync(it => it.child, it => it.parentId, 0, it => it.id);
            }
            else
            {
                return await _client.Context.Queryable<Menu>().LeftJoin<RoleJoin>((it,t)=>it.id==t.menuId).Where((it,t) => it.isDel == 0&&t.roleId==roleId)
                      .Select((it) => new Menu()
                      {
                          id = it.id.SelectAll()
                      })
                      .OrderByDescending(it => it.px)
                      .ToTreeAsync(it => it.child, it => it.parentId, 0, it => it.id);
            }
        }

        /// <summary>
        /// 获取子集列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menu>> GetChildListAsync(int parentId)
        {
            return await _client.GetListAsync(a => a.parentId == parentId);
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <returns></returns>
        public async Task<Menu> GetEntityAsync(int menuId) 
        {
            return await _client.GetByIdAsync(menuId);
        }
        /// <summary>
        /// 新增一个实体
        /// </summary>
        /// <returns></returns>
        public async Task<bool> InsertAsync(Menu menu) 
        {
            return await _client.InsertAsync(menu);
        }
        /// <summary>
        /// 修改一个实体
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Menu menu)
        {
            return await _client.UpdateAsync(menu);
        }
        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(List<int> ids)
        {
            var a= ids.ToArray();
            dynamic[] arry = ids.Select(i=>(dynamic)i).ToArray();
            return await _client.DeleteByIdsAsync(arry);
        }
        /// <summary>
        /// 根据请求页面路径查询子级列表（1菜单目录，2按钮，3接口）
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="typeid">菜单类型：1菜单目录，2按钮，3接口</param>
        /// <returns></returns>
        public async Task<List<Menu>> GetButtonlist(string url, int typeid, int roleId)
        {
            if (roleId == 0)
            {
                var menu = await _client.GetFirstAsync(x => x.url == url);
                if (menu!=null)
                {
                    return await _client.Context.Queryable<Menu>()
                        .Where(x => x.parentId == menu.id && x.typeId == typeid && x.isDel == 0).ToListAsync();
                }
                else
                {
                    return new List<Menu>();
                }
                // return MDataTable.CreateFrom(maction.Select().ToList<AdminMenu>().
                // Where(s => maction.Select(" url='" + url + "'").ToList<AdminMenu>().Select(h => h.id).
                // Contains(s.parent_id) && s.type_id == typeid && s.is_del == 0).ToList());
            }
            else
            {
                return await _client.Context.Queryable<Menu>().LeftJoin<RoleJoin>((it, t) => it.id == t.menuId)
                    .Where((it, t) => it.isDel == 0 && t.roleId == roleId&& it.url == url && it.isDel == 0 && it.typeId == typeid)
                    .OrderByDescending(it => it.px)
                  .ToListAsync();
            }
           
        }

    }
}
